<?php

namespace PHPSpring\Core;

class AnnotationFactory {

  private static $ANNO_IMPLS = array();

  private static function init () {
    if (empty(self::$ANNO_IMPLS)) {
      $annos = array();
      $files = scandir(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Annotation' . DIRECTORY_SEPARATOR . 'Impl');
      foreach ($files as $file) {
        if (preg_match('/\.php/i', $file)) {
          $name = substr($file, 0, -4);
          $clz = '\\PHPSpring\\Annotation\\Impl\\' . $name;
          $annos[strtolower($name)] = $clz;
        }
      }
      self::$ANNO_IMPLS = $annos;
    }
    return self::$ANNO_IMPLS;
  }

  public static function exist (string $annoName) {
    if (empty($annoName)) {
      return false;
    }
    self::init();
    return isset(self::$ANNO_IMPLS[strtolower($annoName)]);
  }

  public static function register (string $annotationFullName) {
    if (empty($annotationFullName)) {
      return;
    }
    self::init();
    $clzParts = explode('\\', $annotationFullName);
    $name = strtolower(end($clzParts));
    if (!isset(self::$ANNO_IMPLS[$name])) {
      self::$ANNO_IMPLS[$name] = $annotationFullName;
    }
  }

  public static function get (string $annoName) {
    if (!self::exist($annoName)) {
      return null;
    }
    return self::$ANNO_IMPLS[strtolower($annoName)];
  }

}